# from gtts import gTTS
# from mutagen.id3 import ID3, TIT2
#
#
# # 要转换的文本
# with open('The Magic of Tidying Up.txt',encoding='utf-8') as f:
#     text = f.read()
#
# # 创建 gTTS 对象
# tts = gTTS(text=text, lang='en')
#
# # 保存为 MP3 文件
# filename = "The Magic of Tidying Up.mp3"
# tts.save(filename)
#
# # 添加元数据
# audio = ID3(filename)
# audio["TIT2"] = TIT2(encoding=3, text=text)
# audio.save()
#


from gtts import gTTS
from mutagen.id3 import ID3, TIT2, ID3NoHeaderError

# 要转换的文本
with open('The Magic of Tidying Up.txt', encoding='utf-8') as f:
    text = f.read()

# 创建 gTTS 对象
tts = gTTS(text=text, lang='en')

# 保存为 MP3 文件
filename = "The Magic of Tidying Up.mp3"
tts.save(filename)

try:
    # 尝试打开已有的 ID3 标签
    audio = ID3(filename)
except ID3NoHeaderError:
    # 如果文件没有 ID3 标签，创建一个新的 ID3 对象
    audio = ID3()

# 添加元数据
audio["TIT2"] = TIT2(encoding=3, text=text)

# 保存修改后的 ID3 标签到文件
audio.save(filename)
